package com.amnc.client.threadPackage;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;

import com.amnc.cattle.model.ChangeGrpPO;
import com.amnc.common.dif.DIFSheet;
import com.amnc.common.dif.exception.DIFDataException;
import com.amnc.common.dif.exception.DIFDataPairParsingException;
import com.amnc.common.dif.exception.DIFKeywordException;
import com.amnc.common.dif.exception.DIFNumberPairInfoException;
import com.amnc.common.dif.exception.DIFStringFormatException;
import com.amnc.common.util.SendDifData;
import com.amnc.service.ServiceDIfReader;

public class ChangeGroupThread implements Runnable{
	private ChangeGrpPO changeGrpPO=new ChangeGrpPO(); 
	
	private String sendFilesPath; //发送文件的路径
	private String fileName; //只要有流产则是代表 流产dif文件
	private String fileFlagString; //文件标识  例如 processing
	private DIFSheet sheet;
	private  int columnMax;
	
	
	
	
	public ChangeGroupThread( String sendFilesPath,
			String fileName, String fileFlagString,
			int columnMax) {
		super();
		this.sendFilesPath = sendFilesPath;
		this.fileName = fileName;
		this.fileFlagString = fileFlagString;
		this.columnMax = columnMax;
	}




	@Override
	public void run() {
		// TODO Auto-generated method stub
		ServiceDIfReader serviceDIfReader=new ServiceDIfReader();
		try {
			while (true) {
				sheet=serviceDIfReader.getDifReader(sendFilesPath, fileName);
				sendAbortion(sheet,columnMax,sendFilesPath,fileName,fileFlagString);
				//睡眠100秒
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		} catch (DIFDataException | DIFKeywordException | DIFDataPairParsingException | DIFNumberPairInfoException | DIFStringFormatException | IOException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	/**
	 * 
	 * @param sheet
	 * @param columnMax 代表dif文件列数
	 * @param sendFilesPath 代表发送文件夹路径
	 * @param fileName  代表发送文件名称关键字 
	 * @param fileFlagString  代表发送标识 比如采用默认的processing
	 * @throws DIFDataException  
	 */
	public void sendAbortion(DIFSheet sheet,int columnMax,String sendFilesPath,String fileName,String fileFlagString) throws DIFDataException{
		String tempNamesString=null;
		File fileFlagFile=null;
		String repeat="0";	//是否是重新发送的 标识，用于服务端;“0”代表第一次发送，“1”代表重复发送。
		File sendFile=new File(sendFilesPath);
		File fileList[]=sendFile.listFiles();
		//根据是否现存 标识文件，来告诉server是否是重复发送
		for (int i = 0; i < fileList.length; i++) {
			if (fileList[i].toString().indexOf(fileName)!=-1 && fileList[i].toString().indexOf(fileFlagString)!=-1) {
				repeat="1";
				tempNamesString=fileList[i].toString();
				break;
			}
		}
		
		if ("0".equals(repeat)) {
			for (int i = 0; i < fileList.length; i++) {
				if (fileList[i].toString().indexOf(fileName)!=-1) {
					fileFlagFile=new File(fileList[i].toString()+".processing");
					try {
						fileFlagFile.createNewFile();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.out.println("processing file can't be created");
					}
					break;
				}
			}
		}else {
			fileFlagFile=new File(tempNamesString);
		}
		
		String[] rows = new String[columnMax + 1];
		int rowsN = sheet.getRows().size();
		for (int i = 2; i < rowsN; i++) {
			for (int j = 1; j <= columnMax; j++) {
				String cellValue = sheet.getRow(i).getCell(j)
						.getStringCellValue();
				rows[j] = cellValue;
			}

			// 流产日期不能为空
			if (!"".equals(rows[3]) && rows[3] != null) {
				changeGrpPO.setFarmId(rows[1].toString());
				changeGrpPO.setCowId(rows[2].toString());
				changeGrpPO.setDate(rows[3]);
				changeGrpPO.setNewGroup(rows[4]);
				changeGrpPO.setChangeGroupComment(rows[5]);
				changeGrpPO.setResponsibleName(rows[6]);
				/**
				 */
//				String url = "http://192.168.1.2:8080/amnc-integration-afifarm-1.0-SNAPSHOT/event/changegroup";
				String 	 url="http://localhost:8080/amnc-integration-afifarm-1.0-SNAPSHOT/event/changegroup";
				SendDifData<ChangeGrpPO> sendDifData=new SendDifData<ChangeGrpPO>();
				sendDifData.sendData(changeGrpPO, url);
			}
		}
		if (fileFlagFile.exists()) {
			//发送完毕，删除标识文件
			fileFlagFile.delete();
		}
		
	}
	
}
